package com.central.test

import java.util

import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}
import com.central.controller._
import com.central.service.{NewClueService, TrailService}
import com.central.utils.LoadESData
import org.apache.spark.sql.SparkSession

object TestTrail {
  def main(args: Array[String]): Unit = {

    /**
      * json格式:{"jobId":"123", "threshold":"and/or/not",
      * "conditions":[
      * {\"deviceids\":\"195\",\"continue\":5,\"begintime\":\"2019-12-01 00:00:00\"},
      * {\"deviceids\":\"202\",\"continue\":5,\"begintime\":\"2019-12-01 00:00:00\"},
      * {\"deviceids\":\"196\",\"continue\":5,\"begintime\":\"2019-12-01 00:00:00\"}
      * ]}
      */

    try {
      val jspara = new JSONObject()
      jspara.put("jobId", "1211")
      jspara.put("threshold", 3)

      jspara.put("hasliferow", "0")
      jspara.put("fuzzyway", "1")

      var paras: JSONArray = new JSONArray()

      var acttime: JSONArray = new JSONArray()
      val time: JSONObject = new JSONObject()
      time.put("days","2020-03-01~2020-03-11")
      time.put("time","10:43:06~11:43:06")
      acttime.add(time)

      jspara.put("activitytimes", acttime)
      for (i <- 0 until (5)) {
//        var a: JSONArray = new JSONArray()
        val jp: JSONObject = new JSONObject()
        jp.put("continue", 15)
        jp.put("begintime", 1583721929000L)
        //        jp.put("threshold", "2")
        //        jp.put("didAppear", "1")
        if (i==0) {
          jp.put("deviceids", "29")
        }
          if (i==1){
            jp.put("deviceids", "32")
          }
        if (i==2){
          jp.put("deviceids", "21")
        }
        if (i==3){
          jp.put("deviceids", "22")
        }
        if (i==4){
          jp.put("deviceids", "5")
        }
        paras.add(jp)
      }
      jspara.put("conditions", paras)
      println(jspara.toJSONString)
      if (args.length == 0) {
        println("-------------------")
        val jsb: JSONObject = JSON.parseObject(jspara.toJSONString)
        val jobId = jsb.getString("jobId")
        val threshold = jsb.getInteger("threshold")
        val jsa: JSONArray = jsb.getJSONArray("conditions")
        val hasliferow = jsb.getString("hasliferow")
        val fuzzyway = jsb.getString("fuzzyway")
        import scala.collection.JavaConversions._
        val activityTimes = jsb.getJSONArray("activitytimes").toArray().map(jobj => {
          val timeMap = jobj.asInstanceOf[util.Map[String, Object]]
          val day = String.valueOf(timeMap.get("days"))
          val time = String.valueOf(timeMap.get("time"))
          ActivityTime(day, time)
        }).toList

        val conditions = jsa
            .toList
          .map(conditionNode => {

            val map = conditionNode.asInstanceOf[util.Map[String, Object]]
            val deviceids = String.valueOf(map.get("deviceids"))
            val deviceIdList = deviceids.split(",").toList
            val continue: Integer = java.lang.Integer.parseInt(map.get("continue") + "")
            val begintime = String.valueOf(map.get("begintime"))
            OnePointTrailConditionEntity(deviceIdList, continue, begintime)
          })
        val param = TrailConditionEntity(jobId.toInt, threshold, conditions,activityTimes,hasliferow,fuzzyway)
        //        var seq: Seq[String] = Seq[String]()
        //        for (i <- 0 until (jsa.size())) {
        //          seq = seq :+ (jsa.get(i) + "")
        //        }
        val properties = LoadESData.loadProperties();
        val para1 = SparkSession
          .builder()
          .appName(jobId)
          //          .master("spark://10.68.2.240:7077")
          .master("local")
          .config("es.nodes", properties.getProperty("elasticsearch.host", "192.168.5.180"))
          .config("es.port", properties.getProperty("elasticsearch.port", "9200"))
          .config("es.mapping.date.rich", "false")
          .getOrCreate()
        para1.sparkContext.setLogLevel("ERROR")
        new TrailService().trailAnalysis(para1, param)
      } else {
        //        log.error("参数个数不对，任务参数应放置在一个Json字符串里")
      }

    } catch {
      case e: Exception => {
        //        log.error(e.getMessage)
      }
    }

  }

}
